package com.letoken.platform.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letoken.platform.admin.service.BlockchainService;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.req.QueryReqBase;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.pub.util.UUIDUtil;
import com.letoken.platform.repo.admin.req.blockchain.AddBlockchainReq;
import com.letoken.platform.repo.admin.req.blockchain.DeleteBlockchainReq;
import com.letoken.platform.repo.admin.req.blockchain.UpdateBlockchainReq;
import com.letoken.platform.repo.admin.res.blockchain.QueryBlockchainListRes;
import com.letoken.platform.repo.app.dao.BlockchainMapper;
import com.letoken.platform.repo.app.manager.BlockchainManager;
import com.letoken.platform.repo.app.po.Blockchain;
import com.letoken.platform.repo.awss3.service.AWSS3Service;
import com.letoken.platform.repo.utlis.PageDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * @author wyl
 * @date 2023/4/18 11:07
 * @Version 1.0
 */
@Service
public class BlockchainServiceImpl extends ServiceImpl<BlockchainMapper, Blockchain> implements BlockchainService {

    @Autowired
    private BlockchainMapper blockchainMapper;

    @Autowired
    private BlockchainManager blockchainManager;

    @Autowired
    private AWSS3Service aWSS3Service;

    @Value("${aws-s3.blockchainLogo:leTokenAdmin/blockchain/logo/}")
    private String blockchainLogo;

    @Override
    public PageData<QueryBlockchainListRes> queryBlockchainList(QueryReqBase req) {
        Page<QueryBlockchainListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        return PageDataUtil.getPageData(blockchainMapper.queryBlockchainList(page));

    }

    @Override
    public void addBlockchain(AddBlockchainReq req) {
        Blockchain add = new Blockchain();
        add.setCreateTime(LocalDateTime.now());
        BeanUtil.quickCopy(req, add);
        add.insert();
    }

    @Override
    public void updateBlockchain(UpdateBlockchainReq req) {
        Blockchain update = new Blockchain();
        BeanUtil.quickCopy(req, update);
        update.updateById();
    }

    @Override
    public void deleteBlockchain(DeleteBlockchainReq req) {
        blockchainMapper.deleteBatchIds(req.getIdList());
    }

    @Override
    public QueryBlockchainListRes queryBlockchainById(Integer id) {
        Blockchain blockchain = blockchainMapper.selectById(id);
        QueryBlockchainListRes res = new QueryBlockchainListRes();
        BeanUtil.quickCopy(blockchain, res);
        return res;
    }

    @Override
    public String uploadLogo(MultipartFile file) {
        String contentType = file.getContentType();
        if (StringUtils.isNotBlank(contentType)) {
            if (contentType.contains("image")) {
                String key = blockchainLogo + UUIDUtil.getUUID(16);
                return aWSS3Service.uploadFile(file, key);
            }
        }
        throw new ApiServiceException("系统错误，请稍后再试！");
    }
}
